package io.renren.modules.sp.controller;

import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.math.BigInteger;

import static org.apache.poi.hslf.usermodel.HSLFTextParagraph.setText;

public class WordUtil {
    /**
    *添加文字
    */
    public static XWPFDocument addWord( XWPFDocument document,String word) {
        XWPFParagraph paragraph = document.createParagraph();
        // 创建一个运行块，可以理解为一个文本区块
        XWPFRun run = paragraph.createRun();
        run.setText(word);
        run.setFontSize(14);
        return document;
    }
    /**
    *添加表格
    */
    public static XWPFTable addTable(XWPFDocument document,String[] head) {
        XWPFTable table = document.createTable();
        //隐藏左边边框
        table.setLeftBorder(XWPFTable.XWPFBorderType.NIL, 1, 0, "000000");
        table.setRightBorder(XWPFTable.XWPFBorderType.NIL, 1, 0, "000000");
        table.setLeftBorder(XWPFTable.XWPFBorderType.NIL, 1, 1, "000000");
        table.setRightBorder(XWPFTable.XWPFBorderType.NIL, 1, 1, "000000");

        XWPFTableRow tableHeader = table.getRow(0); // 获取第一行
        table.setWidth((int)(566.8934*5.3));
        //17.64
        for (int i = 0; i < head.length; i++) {
            XWPFTableCell cell =null;
            if(i == 0 ){
                cell = tableHeader.getCell(0);
                cell.setWidth(String.valueOf((int)(566.8934*1.8)));
                cell.setWidthType(TableWidthType.DXA);
            }else{
                cell = tableHeader.addNewTableCell();
                cell.setWidth(String.valueOf((int)(566.8934*3.5)));
                cell.setWidthType(TableWidthType.DXA);

            }

            setCellContentCentered(cell,head[i],"楷体",10,true);
            CTTcBorders borders =  cell.getCTTc().addNewTcPr().addNewTcBorders();
            //隐藏右边的边框
            borders.addNewRight().setVal(STBorder.NIL);
            borders.addNewLeft().setVal(STBorder.NIL);
        }
        return table;
    }

    /**
     *添加表格
     */
    public static XWPFTable addTableRow(XWPFTable table,String[] cells) {

        XWPFTableRow dataRow = table.createRow();
        for (int i = 0; i < cells.length; i++) {
            XWPFTableCell cell2 = dataRow.getCell(i);
            setCellContentCentered(cell2,cells[i],"楷体",10,false);

            CTTcBorders borders =  dataRow.getCell(i).getCTTc().addNewTcPr().addNewTcBorders();
            //隐藏右边的边框
            borders.addNewRight().setVal(STBorder.NIL);
            borders.addNewLeft().setVal(STBorder.NIL);
            if(i == 0 ){
                XWPFTableCell cell = dataRow.getCell(i);
                cell.setWidth(String.valueOf((int)(566.8934*1.8)));
                cell.setWidthType(TableWidthType.DXA);

            }else{
                XWPFTableCell cell =  dataRow.getCell(i);
                cell.setWidth(String.valueOf((int)(566.8934*3.5)));
                cell.setWidthType(TableWidthType.DXA);

            }

        }

        return table;
    }

    public  static void setRepeatHeaderRow(XWPFTable table) {
        CTTrPr trPr = table.getRow(0).getCtRow().addNewTrPr();
        CTOnOff repeatHeader = trPr.addNewTblHeader();
        repeatHeader.setVal(STOnOff.ON);
    }

    private static void setCellContentCentered(XWPFTableCell cell, String text, String fontName, int fontSize, boolean bold) {
        // 清除单元格原有内容
        cell.removeParagraph(0);

        // 创建一个段落并设置段落居中
        XWPFParagraph paragraph = cell.addParagraph();
        paragraph.setAlignment(ParagraphAlignment.CENTER);


        // 创建一个运行并设置文本
        XWPFRun run = paragraph.createRun();
        run.setFontFamily(fontName);
        run.setFontSize(fontSize);
        run.setBold(bold);
        run.setText(text);
    }
}
